<template>
  <view class="page-address-addEdit">
    <van-cell-group :border="false">
      <van-field label="姓名" :value="form.custName" placeholder="请输入姓名" input-align="right" type="textarea" @input="e => form.custName = e.detail"/>
      <fly-actionSheet title="性别"  :value.sync="form.sex" :z-index="101" input-align="right" :list="initData.sexList" is-link/>        
      <van-field label="年龄" :value="form.age" placeholder="请输入年龄" type="number" input-align="right" @input="e => form.age = e.detail"/>      
      <van-field label="身份证号码" :value="form.idNo" placeholder="请输入身份证号码" input-align="right" @input="e => form.idNo = e.detail"/>
      <van-field label="联系电话" :value="form.linkmanPhone" placeholder="请输入联系电话" input-align="right" @input="e => form.linkmanPhone = e.detail"/>
      <van-field label="联系手机" :value="form.phone" placeholder="请输入联系手机" input-align="right" @input="e => form.phone = e.detail"/>
      <fly-area title="所属省市" :value.sync="form.zoneName" :area-names="form.zoneName" input-align="right" @confirm="getArea"/>
      <van-field label="详细地址" :value="form.address" placeholder="请输入详细地址" input-align="right" @input="e => form.address = e.detail"/>
    </van-cell-group>
    <view class="p-a-15">
      <van-button type="info" block :loading="btnLoading" @click="submitForm">保存</van-button>
    </view>
  </view>
</template>
<script>
  import flyActionSheet from '@/components/flyActionSheet';
  import flyArea from '@/components/flyArea';
  export default {
    data(){
      return{
        pageParams:{},
        btnLoading: false,
        methods: 'post',
        api: '/nursing/vcs/vip/cust',
        form:{
						custId: null,
						custName:'',
						sex:'',
						age:'',
						idNo:'',
						linkmanPhone:'',
						phone:'',
						address:'',
						zoneId: null,
            zoneName: null,
            birthDate: null,
					},
          initData:{
            sexList:[
              {id: '男', name: '男'},
              {id: '女', name: '女'},
            ]
          }
      }
    },
    components:{
      flyActionSheet,
      flyArea
    },
    onLoad(e){
      this.pageParams = e;
      if(this.pageParams.custId){
        this.getCustInfo();
      }
    },
    onShow(){

    },
    methods:{
      getCustInfo(){
        this.$http('get','/nursing/vcs/vip/cust/chronic', {custId: this.pageParams.custId}, (data,msg) => {
          this.$utils.resetForm(this.form, data);
          this.methods = 'put';
        })
      },
      getArea(data){
        let len = data.length;
        console.log(data);
        this.form.zoneId = data[len-1].code;
        this.form.zoneName = data[len-1].name;
      },
      verifyForm(){
        if(!this.$verify.null(this.form.zoneId, '请选择省市区')){
          return false;
        }
        if(!this.$verify.null(this.form.address, '请填写门牌号')){
          return false;
        }
        if(!this.$verify.null(this.form.linkmanPhone, '请填写联系电话')){
          return false;
        }
        if(!this.$verify.mobile(this.form.phone, '请填写正确的手机号')){
          return false;
        }
        if(!this.$verify.identity(this.form.idNo, '请填写正确的身份证号码')){
          return false;
        }
        this.form.birthDate = this.form.idNo.slice(6,10)+ '-' + this.form.idNo.slice(10,12) + '-'+this.form.idNo.slice(12,14);
        return true;
      },
      submitForm(){
        if(!this.verifyForm()){
          return;
        }
        this.btnLoading = true;
        this.$http(this.methods, this.api, this.form, (data,msg) => {
          uni.showToast({title: '提交成功'});
          setTimeout(() => {
            this.btnLoading = false;
            uni.navigateBack();
          },1500)
        },msg => {
          this.btnLoading = false;
        })
      }
    }
  }
</script>
<style>
  page{background:#fff;}
</style>